    //
//  RS1ViewController.m
//  SmartRoadShow
//
//  Created by Tracy E on 10-10-30.
//  Copyright 2010 simonwang142232@gmail.com. All rights reserved.
//

#import "RS1ViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "SendHttpRequest.h"
#import "MyDataSource.h"
#import "CacheManager.h"
#import "WebImageView.h"
#import "ArtEvent.h"
#import "ArtMark.h"
#import "ArtClass.h"
#import "DetailWebViewController.h"
#import "Reachability.h"
#import "Constants.h"


@interface RS1ViewController()
- (void)loadEventScrollView:(NSArray *)eventArr Back:(BOOL) back;
- (void)loadMarkScrollView:(NSArray *)markArr Back:(BOOL) back;
- (void)noDataAlertType:(NSInteger) type;
- (BOOL)showAlertWhileNoConnection;
@end

enum  {
	kEventCityPicker = 801,
    kEventTimePicker = 802,
	kMarkCityPicker = 803,
	kMarkTypePicker = 804,
};


@implementation RS1ViewController
@synthesize segmentControl;
@synthesize selectedEventCity,selectedMarkType,selectedMarkCity,eventArray;

#pragma mark ----------------------------------------------
#pragma mark Actions
- (NSInteger)getNumberOfDaysInMonth:(NSInteger) month{
	switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			return 31;
			break;
		case 2:
			return 28;
			break;
		case 4:
		case 6:
		case 9:		
		case 11:
            return 30;
			break;
		default:
			break;
	}
	return 0;
}

//根据选择的不同月，动态改变日的天数
- (void)reloadTimePickerDayComponent:(NSInteger) month{
	[dayArray removeAllObjects];
	for (int i = 1; i <= [self getNumberOfDaysInMonth:month]; i++) {
		[dayArray addObject:[NSString stringWithFormat:@"%d",i]];
	}
	[eventTimePickerView reloadComponent:2];
}


#pragma mark TESegmentedControl Delegate

- (void)segmentedControl:(TESegmentedControl *)segmentedControl segmentDidChangeToIndex:(NSInteger)index{
    
    //picker view从屏幕上移开
	[eventCityPickerBG removeFromSuperview];
	[eventTimePickerBG removeFromSuperview];
	[markCityPickerBG removeFromSuperview];
	[markTypePickerBG removeFromSuperview];

	switch (index) {
		case 0: //艺术活动
			for (UIView *subview in self.view.subviews){
				if (subview.tag == 101 || subview.tag == 102 || subview.tag == 103) {
					[subview removeFromSuperview];
				}
			}
			[self.view addSubview:eventView];
			break;
		case 1://艺术地表
			for (UIView *subview in self.view.subviews){
				if (subview.tag == 101 || subview.tag == 103) {
					[subview removeFromSuperview];
				}
			}
			[self.view addSubview:markView];
			break;
		case 2: //艺术课堂
			for (UIView *subview in self.view.subviews) {
				if (subview.tag == 101 || subview.tag == 102) {
					[subview removeFromSuperview];
				}
			}
			[self.view addSubview:classTableView];
			break;
		default:
			break;
	}
}

//选择时没有数据时弹出的警告
- (void)noDataAlertType:(NSInteger) type{
    
	NSString *typeString = nil;
	switch (type) {
		case 1:
			typeString = @"城市";
			break;
		case 2:
			typeString = @"时间";
			break;
		case 3:
			typeString = @"城市";
			break;
        case 4:
			typeString = @"类别";
			break;

		default:
			break;
	}
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
													message:[NSString stringWithFormat:@"您选择的%@没数据！",typeString] 
												   delegate:nil
										  cancelButtonTitle:@"确定"
										  otherButtonTitles:nil];
	[alert show];
	[alert release];
}

//艺术课堂“更多”
- (void)moreArtClass{
	classWaitView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(80, 10, 20, 20)];
	[classWaitView setHidesWhenStopped:YES];
	[classWaitView startAnimating];
	[classWaitView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
	[classTableView.tableFooterView addSubview:classWaitView];
	[classWaitView release];
	
	[self performSelector:@selector(getMoreInfoExecute) withObject:nil afterDelay:0.1];
}

- (void)getMoreInfoExecute{
	[classWaitView stopAnimating];
	[classWaitView removeFromSuperview];
	
	currentClassPage++;
	
	NSString *urlstring = [[NSString alloc] initWithFormat:kArtClassMoreInfo];
	NSString *postString = [[NSString alloc] initWithFormat:@"page=%d",currentClassPage];
	NSString *documentStr = [[SendHttpRequest sendHttpPostRequestWithURLString:urlstring postString:postString decodeEncoding:NSUTF8StringEncoding] retain];
	NSArray * tempArr = [[MyDataSource getArtClassInfo:documentStr] retain];
	if ([tempArr count] != 0) {
		[classInfoArray addObjectsFromArray:tempArr];
	}
	[classTableView reloadData];
	[tempArr release];
	[documentStr release];
	[urlstring release];
	[postString release];
}

//转到艺术活动
-(void) gotoDetailView:(id)sender{
	
	if ([self showAlertWhileNoConnection]) {
		int newsId = ((UIButton*)sender).tag - 1999;
		
		ArtEvent *event = [eventArray objectAtIndex: newsId - 1];
		NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:event._content]];
		
		DetailWebViewController *classViewController = [[DetailWebViewController alloc] init];
		[classViewController.webView loadRequest:request];
		[self.navigationController pushViewController:classViewController animated:YES];
		[classViewController release];
		
	}
}

//艺术地标的详细界面
-(void) gotoDetailView2:(id)sender{
	if ([self showAlertWhileNoConnection]) {
		int newsId = ((UIButton*)sender).tag - 2999;
		
		ArtMark *mark = [markArray objectAtIndex:newsId - 1];
		NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:mark._content]];
		
		DetailWebViewController *classViewController = [[DetailWebViewController alloc] init];
		[classViewController.webView loadRequest:request];
		[self.navigationController pushViewController:classViewController animated:YES];
		[classViewController release];
		
	}
}

#pragma mark -----------------------------------------------
#pragma mark -select action
//艺术地图“选择城市”
- (void)selectEventCity{
	if (eventCityPickerView == nil) {
		eventCityPickerBG = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 320, 300)];
		eventCityPickerBG.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.95];
		eventCityPickerBG.layer.cornerRadius = 10;
		
		UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 14, 280, 20)];
		[titleLabel setTextAlignment:UITextAlignmentCenter];
		[titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
		[titleLabel setBackgroundColor:[UIColor clearColor]];
		[titleLabel setText:@"请选择您要参加的城市"];
		[titleLabel setTextColor:[UIColor colorWithRed:0.95 green:0.83 blue:0.16 alpha:1]];
		[eventCityPickerBG addSubview:titleLabel];
		[titleLabel release];
		
		if (cityArray == nil) {
			cityArray = [[NSArray alloc] initWithObjects:@"全部",@"北京",@"上海",@"广州",@"沈阳",@"大连",@"无锡",@"温州",
						 @"深圳",@"杭州",@"成都",@"重庆",@"厦门",@"南京",nil];
		}

		eventCityPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, 250, 200)];
		[eventCityPickerView setTag:kEventCityPicker];
		[eventCityPickerView setBackgroundColor:[UIColor clearColor]];
		eventCityPickerView.layer.borderWidth = 10;
		eventCityPickerView.layer.borderColor = [[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.95] CGColor];
		[eventCityPickerView setDelegate:self];
		[eventCityPickerView setDataSource:self];
		[eventCityPickerView setShowsSelectionIndicator:YES];
		[eventCityPickerBG addSubview:eventCityPickerView];
		[eventCityPickerView release];
		
		UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
		selectButton.frame = CGRectMake(58, 260, 200, 30);
		selectButton.titleLabel.font = [UIFont boldSystemFontOfSize:13];
		[selectButton setBackgroundImage:[UIImage imageNamed:@"button1.png"] forState:UIControlStateNormal];
		[selectButton setTitle:@"确定" forState:UIControlStateNormal];
		[selectButton addTarget:self action:@selector(finishedSelectEventCity) forControlEvents:UIControlEventTouchUpInside];
		selectButton.titleLabel.textAlignment = UITextAlignmentCenter;
		[eventCityPickerBG addSubview:selectButton];
	}
	[eventView addSubview:eventCityPickerBG];
}

//艺术地图“选择时间”
- (void)selectEventTime{
	
	allTimeFlag = NO;
	
	NSCalendar* calendar = [[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar] autorelease];
	NSDateComponents* comps = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |
							   NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | 
							   NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];
	int year = comps.year;
	int month = comps.month;
	int day = comps.day;

	selectedBeginYear = year;
	selectedBeginMonth = month;
	selectedBeginDay = day;
	
	if (dayArray == nil) {
		dayArray = [[NSMutableArray alloc] init];
	}
	[self reloadTimePickerDayComponent:month];
	
	if (eventTimePickerView == nil) {
		eventTimePickerBG = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 320, 300)];
		eventTimePickerBG.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.95];
		eventTimePickerBG.layer.cornerRadius = 10;
		
		timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 14, 280, 20)];
		[timeLabel setTextAlignment:UITextAlignmentCenter];
		[timeLabel setFont:[UIFont boldSystemFontOfSize:13]];
		[timeLabel setBackgroundColor:[UIColor clearColor]];
		[timeLabel setText:@"请选择开始时间"];
		[timeLabel setTextColor:[UIColor colorWithRed:0.95 green:0.83 blue:0.16 alpha:1]];
		[eventTimePickerBG addSubview:timeLabel];
		[timeLabel release];
		
		if (yearArray == nil) {
			yearArray = [[NSArray alloc] initWithObjects:@"2009",@"2010",@"2011",@"2012",@"2013",@"2014",@"2015",nil];
			monthArray = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",nil];
		}
		
		eventTimePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, 250, 200)];
		[eventTimePickerView setTag:kEventTimePicker];
		[eventTimePickerView setBackgroundColor:[UIColor clearColor]];
		eventTimePickerView.layer.borderWidth = 10;
		eventTimePickerView.layer.borderColor = [[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.95] CGColor];
		[eventTimePickerView setDelegate:self];
		[eventTimePickerView setDataSource:self];
		[eventTimePickerView setShowsSelectionIndicator:YES];		
		[eventTimePickerBG addSubview:eventTimePickerView];
		[eventTimePickerView release];
	}
	//时间picker每次都是从今天的日期开始
	int row_year = year-2009;
	int row_month = month-1;
	
	[eventTimePickerView selectRow:row_year inComponent:0 animated:NO];
	[eventTimePickerView selectRow:row_month inComponent:1 animated:NO];
	[eventTimePickerView selectRow:day-1 inComponent:2 animated:NO];
	
	for (UIView *subview in eventTimePickerBG.subviews){
		if (subview.tag == 211 || subview.tag == 222) {
			[subview removeFromSuperview];
		}
	}
	
	UIButton *allTime = [UIButton buttonWithType:UIButtonTypeCustom];
	[allTime setTag:211];
	allTime.frame = CGRectMake(50, 260, 100, 30);
	[allTime setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
	[allTime setTitle:@"选择全部时间" forState:UIControlStateNormal];
	[allTime addTarget:self action:@selector(finishSelectedAllTime) forControlEvents:UIControlEventTouchUpInside];
	allTime.titleLabel.font = [UIFont boldSystemFontOfSize:13];
	[eventTimePickerBG addSubview:allTime];
	
	UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
	selectButton.frame = CGRectMake(170, 260, 100, 30);
	selectButton.tag = 222;
	selectButton.titleLabel.font = [UIFont boldSystemFontOfSize:13];
	[selectButton setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
	[selectButton setTitle:@"下一步" forState:UIControlStateNormal];
	[selectButton addTarget:self action:@selector(finishSelectEventTime:) forControlEvents:UIControlEventTouchUpInside];
	selectButton.titleLabel.textAlignment = UITextAlignmentCenter;
	[eventTimePickerBG addSubview:selectButton];
	
	
	[eventView addSubview:eventTimePickerBG];
	
}



//艺术地标“选择城市”
- (void)selectMarkCity{
	if (markCityPickerView == nil) {
		markCityPickerBG = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 320, 300)];
		markCityPickerBG.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.95];
		markCityPickerBG.layer.cornerRadius = 10;
		
		UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 14, 280, 20)];
		[titleLabel setTextAlignment:UITextAlignmentCenter];
		[titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
		[titleLabel setBackgroundColor:[UIColor clearColor]];
		[titleLabel setText:@"请选择您要参加的城市"];
		[titleLabel setTextColor:[UIColor colorWithRed:0.95 green:0.83 blue:0.16 alpha:1]];
		[markCityPickerBG addSubview:titleLabel];
		[titleLabel release];
		
		if (cityArray == nil) {
			cityArray = [[NSArray alloc] initWithObjects:@"全部",@"北京",@"上海",@"广州",@"沈阳",@"大连",@"无锡",@"温州",
						 @"深圳",@"杭州",@"成都",@"重庆",@"厦门",@"南京",nil];
		}
		
		markCityPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, 250, 200)];
		[markCityPickerView setTag:kMarkCityPicker];
		[markCityPickerView setBackgroundColor:[UIColor clearColor]];
		markCityPickerView.layer.borderWidth = 10;
		markCityPickerView.layer.borderColor = [[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.95] CGColor];
		[markCityPickerView setDelegate:self];
		[markCityPickerView setDataSource:self];
		[markCityPickerView setShowsSelectionIndicator:YES];
		[markCityPickerBG addSubview:markCityPickerView];
		[markCityPickerView release];
		
		UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
		selectButton.frame = CGRectMake(58, 260, 200, 30);
		selectButton.titleLabel.font = [UIFont boldSystemFontOfSize:13];
		[selectButton setBackgroundImage:[UIImage imageNamed:@"button1.png"] forState:UIControlStateNormal];
		[selectButton setTitle:@"确定" forState:UIControlStateNormal];
		[selectButton addTarget:self action:@selector(finishedSelectMarkCity) forControlEvents:UIControlEventTouchUpInside];
		selectButton.titleLabel.textAlignment = UITextAlignmentCenter;
		[markCityPickerBG addSubview:selectButton];
	}
	[markView addSubview:markCityPickerBG];
}

//艺术地标“选择类别”
- (void)selectMarkType{
	if (markTypePickerView == nil) {
		markTypePickerBG  = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 320, 300)];
		markTypePickerBG.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.95];
		markTypePickerBG.layer.cornerRadius = 10;
		
		UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 14, 280, 20)];
		[titleLabel setTextAlignment:UITextAlignmentCenter];
		[titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
		[titleLabel setBackgroundColor:[UIColor clearColor]];
		[titleLabel setText:@"请选择艺术地标的类型"];
		[titleLabel setTextColor:[UIColor colorWithRed:0.95 green:0.83 blue:0.16 alpha:1]];
		[markTypePickerBG addSubview:titleLabel];
		[titleLabel release];
		
		typeArray = [[NSArray alloc] initWithObjects:@"全部",@"艺术区",@"艺术展馆",@"艺术商店",@"艺术餐厅",@"其他",nil];
		
		markTypePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, 250, 200)];
		[markTypePickerView setBackgroundColor:[UIColor clearColor]];
		[markTypePickerView setTag:kMarkTypePicker];
		markTypePickerView.layer.borderWidth = 10;
		markTypePickerView.layer.borderColor = [[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.95] CGColor];
		[markTypePickerView setDelegate:self];
		[markTypePickerView setDataSource:self];
		[markTypePickerView setShowsSelectionIndicator:YES];
		[markTypePickerBG addSubview:markTypePickerView];
		[markTypePickerView release];
		
		UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
		selectButton.frame = CGRectMake(58, 260, 200, 30);
		selectButton.titleLabel.font = [UIFont boldSystemFontOfSize:13];
		[selectButton setBackgroundImage:[UIImage imageNamed:@"button1.png"] forState:UIControlStateNormal];
		[selectButton setTitle:@"确定" forState:UIControlStateNormal];
		[selectButton addTarget:self action:@selector(finishedSelectMarkType) forControlEvents:UIControlEventTouchUpInside];
		selectButton.titleLabel.textAlignment = UITextAlignmentCenter;
		[markTypePickerBG addSubview:selectButton];
	}
	[markView addSubview:markTypePickerBG];
}

#pragma mark -------finished select

- (BOOL)showAlertWhileNoConnection{
	Reachability *internetReach = [Reachability reachabilityForInternetConnection];
	[internetReach startNotifier];	
	NetworkStatus netStatus = [internetReach currentReachabilityStatus];
	
	if (netStatus) {
		return YES;
	}
	else {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"本功能需要网络支持，请检查网络连接！"
													   delegate:nil cancelButtonTitle:@"确定"
											  otherButtonTitles:nil];
		[alert show];
		[alert release];
		return NO;
	}
}



//艺术活动－－选择城市完后－－查询数据库－－重新加载scrollView
- (void)finishedSelectEventCity
{
	[eventCityPickerBG removeFromSuperview];

	[self.view bringSubviewToFront:waitView];
	[waitView showWhileExecuting:@selector(finishedSelectEventCityExe) onTarget:self
					  withObject:nil animated:YES];
}

- (void)finishedSelectEventCityExe{
	
	eventPostPage = 0;

	NSString *beginStr = [[NSString alloc] initWithFormat:@"%04d-%02d-%02d",selectedBeginYear,selectedBeginMonth,selectedBeginDay];
	NSString *endStr = [[NSString alloc] initWithFormat:@"%04d-%02d-%02d",selectedEndYear,selectedEndMonth,selectedEndDay];
	NSString *urlString = [[NSString alloc] initWithFormat:kArtEventSearch];
	NSString *postStr = nil;
	
	
	if ([self.selectedEventCity isEqualToString:@"全部"]) {
		if (allTimeFlag) {
			if ([ArtEvent findCity:selectedEventCity Begintime:beginStr Endtime:endStr] != 0) {
				[eventArray release];
				eventArray = [(NSMutableArray *)[ArtEvent findCity:selectedEventCity Begintime:beginStr Endtime:endStr] retain];
				[self loadEventScrollView:eventArray Back:YES];
			}
			else {
				[self noDataAlertType:1];
			}
		}
		else if([self showAlertWhileNoConnection]){
			postStr = [NSString stringWithFormat:@"page=0&starttime=%@&endtime=%@",beginStr,endStr];
			NSString *documentStr = [[SendHttpRequest sendHttpPostRequestWithURLString:urlString postString:postStr decodeEncoding:NSUTF8StringEncoding] retain];
						
			if ([[MyDataSource artEventGetDataFromXMLDocument:documentStr IntoDB:NO] count] != 0) {
				[eventArray release];
				eventArray = [[MyDataSource artEventGetDataFromXMLDocument:documentStr IntoDB:NO] retain];
				[self loadEventScrollView:eventArray Back:YES];
			}
			else {
				[self noDataAlertType:1];
			}
			[documentStr release];
		}
		
	}
	else if([self showAlertWhileNoConnection]){
		if (allTimeFlag) {
			postStr = [NSString stringWithFormat:@"page=0&city=%@",self.selectedEventCity];
			
		}
		else {
			postStr = [NSString stringWithFormat:@"page=0&city=%@&starttime=%@&endtime=%@",self.selectedEventCity,beginStr,endStr];
			
		}
		NSString *documentStr = [[SendHttpRequest sendHttpPostRequestWithURLString:urlString postString:postStr decodeEncoding:NSUTF8StringEncoding] retain];
				
		if ([[MyDataSource artEventGetDataFromXMLDocument:documentStr IntoDB:NO] count] != 0) {
			[eventArray release];
			eventArray = [[MyDataSource artEventGetDataFromXMLDocument:documentStr IntoDB:NO] retain];
			[self loadEventScrollView:eventArray Back:YES];
		}
		else {
			[self noDataAlertType:1];
		}
		[documentStr release];
	}
	[beginStr release];
	[endStr release];
	[urlString release];
}

- (void)finishSelectedAllTime{
	[eventTimePickerBG removeFromSuperview];
	[self.view bringSubviewToFront:waitView];
	[waitView showWhileExecuting:@selector(finishSelectedAllTimeExe) onTarget:self
				  withObject:nil animated:YES];
}


- (void)finishSelectedAllTimeExe{
	
	eventPostPage = 0;
    allTimeFlag = YES;
	
	NSString *urlString = [NSString stringWithFormat:kArtEventSearch];
	NSString *postStr = nil;
	
	if (![self.selectedEventCity isEqualToString:@"全部"] && [self showAlertWhileNoConnection]) {
		postStr = [NSString stringWithFormat:@"city=%@&page=0",self.selectedEventCity];
		NSString *documentStr = [[SendHttpRequest sendHttpPostRequestWithURLString:urlString postString:postStr decodeEncoding:NSUTF8StringEncoding] retain];
		
		if ([[MyDataSource artEventGetDataFromXMLDocument:documentStr IntoDB:NO] count] != 0) {
			[eventArray release];
			eventArray = [[MyDataSource artEventGetDataFromXMLDocument:documentStr IntoDB:NO] retain];
			[self loadEventScrollView:eventArray Back:YES];
		}
		else {
			[self noDataAlertType:2];
		}
		[documentStr release];
		
	}else {
		if ([[ArtEvent findCity:selectedEventCity Begintime:@"0000-00-00" Endtime:@"0000-00-00"] count] != 0) {
			[eventArray release];
			eventArray = [(NSMutableArray *)[ArtEvent findCity:selectedEventCity Begintime:@"0000-00-00" Endtime:@"0000-00-00"] retain];
			[self loadEventScrollView:eventArray Back:YES];
		}
		else {
			[self noDataAlertType:2];
		}
		
	}
}


//艺术活动－－选择时间－－查询数据库－－重新加载scrollView
- (void)finishSelectEventTime:(UIButton *)sender{

	allTimeFlag = NO;
	
	if (isFirst) {
		[timeLabel setText:@"请选择结束时间"];
		isFirst = NO;
		
		for (UIView *subview in eventTimePickerBG.subviews){
			if (subview.tag == 211) {
				[subview removeFromSuperview];
			}
		}
		
		sender.frame = CGRectMake(58, 260, 200, 30);
		[sender setBackgroundImage:[UIImage imageNamed:@"button1.png"] forState:UIControlStateNormal];
		[sender setTitle:@"确定" forState:UIControlStateNormal];
		
		int year = selectedBeginYear;
		int month = selectedBeginMonth;
		int day = selectedBeginDay;
		
		month = month + 3;
		
		if (month > 12) {
			year = year + 1;
			month = month - 12;
		}
		if (day > [self getNumberOfDaysInMonth:month]) {
			day = [self getNumberOfDaysInMonth:month];
		}		
		
		//初始化结束时间 为选择的开始时间
		selectedEndYear = year;
		selectedEndMonth = month;
		selectedEndDay = day;
		
		[eventTimePickerView selectRow:year - 2009 inComponent:0 animated:NO];
		[eventTimePickerView selectRow:month - 1 inComponent:1 animated:NO];
		[eventTimePickerView selectRow:day-1 inComponent:2 animated:NO];

		[self reloadTimePickerDayComponent:month];
	}
	else {
		
		eventPostPage = 0;

		[eventTimePickerBG removeFromSuperview];
		[timeLabel setText:@"请选择开始时间"];
		isFirst = YES;
		
		//这里作查询数据库的操作
		[self.view bringSubviewToFront:waitView];
		[waitView showWhileExecuting:@selector(finishedSelectTimeExe) onTarget:self
					  withObject:nil animated:YES];
				
	}
		
}

- (void)finishedSelectTimeExe{
	
	NSString *beginStr = [NSString stringWithFormat:@"%04d-%02d-%02d",selectedBeginYear,selectedBeginMonth,selectedBeginDay];
	NSString *endStr = [NSString stringWithFormat:@"%04d-%02d-%02d",selectedEndYear,selectedEndMonth,selectedEndDay];
	
	
	
	if ([self showAlertWhileNoConnection]) {
		
		NSString *urlString = [NSString stringWithFormat:kArtEventSearch];
		NSString *postStr = nil;
		
		if ([self.selectedEventCity isEqualToString:@"全部"]) {
			postStr = [NSString stringWithFormat:@"page=0&starttime=%@&endtime=%@",beginStr,endStr];
		}
		else {
			postStr = [NSString stringWithFormat:@"city=%@&page=0&starttime=%@&endtime=%@",self.selectedEventCity,beginStr,endStr];
		}
		NSString *documentStr = [[SendHttpRequest sendHttpPostRequestWithURLString:urlString postString:postStr decodeEncoding:NSUTF8StringEncoding] retain];
		
		if ([[MyDataSource artEventGetDataFromXMLDocument:documentStr IntoDB:NO] count] != 0) {
			[eventArray release];
			eventArray = [[MyDataSource artEventGetDataFromXMLDocument:documentStr IntoDB:NO] retain];
			[self loadEventScrollView:eventArray Back:YES];
		}
		else {
			[self noDataAlertType:2];
		}
		[documentStr release];
	}
	
}

//艺术地标－－完成选择城市－－查询数据库－－重新加载scrollView

- (void)finishedSelectMarkCity{
	[markCityPickerBG removeFromSuperview];
	
	[self.view bringSubviewToFront:waitView];
	[waitView showWhileExecuting:@selector(finishedSelectMarkCityExe) onTarget:self
				  withObject:nil animated:YES];
}

- (void)finishedSelectMarkCityExe{
	
	markPostPage = 0;
	
	NSString *urlString = [NSString stringWithFormat:kArtMarkSearch];
	
	NSString *postStr = nil;
	
	if ([self.selectedMarkCity isEqualToString:@"全部"]) {
		if ([self.selectedMarkType isEqualToString:@"全部"]) {
			[markArray release];
			markArray = [(NSMutableArray *)[ArtMark findCity:@"全部" MarkType:@"全部"] retain];
			[self loadMarkScrollView:markArray Back:YES];
		}
		else if([self showAlertWhileNoConnection]){
			postStr = [NSString stringWithFormat:@"page=0&marktype=%@",self.selectedMarkType];
			
			NSString *markDocString = [[SendHttpRequest sendHttpPostRequestWithURLString:urlString postString:postStr decodeEncoding:NSUTF8StringEncoding] retain];
						
			if ([[MyDataSource artMarkGetDataFromXMLDocument:markDocString IntoDB:NO] count] != 0) {
				[markArray release];
				markArray = [[MyDataSource artMarkGetDataFromXMLDocument:markDocString IntoDB:NO] retain];
				[self loadMarkScrollView:markArray Back:YES];
			}
			else {
				[self noDataAlertType:3];	
			}
			[markDocString release];
		}
	}
	else if([self showAlertWhileNoConnection]){
		if ([self.selectedMarkType isEqualToString:@"全部"]) {
			postStr = [NSString stringWithFormat:@"page=0&city=%@",self.selectedMarkCity];
		}
		else {
			postStr = [NSString stringWithFormat:@"page=0&city=%@&marktype=%@",self.selectedMarkCity,self.selectedMarkType];
			
		}
		NSString *markDocString = [[SendHttpRequest sendHttpPostRequestWithURLString:urlString postString:postStr decodeEncoding:NSUTF8StringEncoding] retain];
		
		if ([[MyDataSource artMarkGetDataFromXMLDocument:markDocString IntoDB:NO] count] != 0) {
			[markArray release];
			markArray = [[MyDataSource artMarkGetDataFromXMLDocument:markDocString IntoDB:NO] retain];
			[self loadMarkScrollView:markArray Back:YES];
		}
		else {
			[self noDataAlertType:3];	
		}
		[markDocString release];
	}
}

//艺术地标－－完成选择类别－－查询数据库－－重新加载scrollView

- (void)finishedSelectMarkType{
	[markTypePickerBG removeFromSuperview];
	[self.view bringSubviewToFront:waitView];
	[waitView showWhileExecuting:@selector(finishedSelectMarkTypeExe) onTarget:self
				  withObject:nil animated:YES];
}

- (void)finishedSelectMarkTypeExe{
	
	markPostPage = 0;
	
	NSString *urlString = [NSString stringWithFormat:kArtMarkSearch];
	NSString *postStr = nil;
	
	if ([self.selectedMarkCity isEqualToString:@"全部"]) {
		if ([self.selectedMarkType isEqualToString:@"全部"]) {
			[markArray release];
			markArray = [(NSMutableArray *)[ArtMark findCity:@"全部" MarkType:@"全部"] retain];
			[self loadMarkScrollView:markArray Back:YES];

		}
		else if([self showAlertWhileNoConnection]){
			postStr = [NSString stringWithFormat:@"page=0&marktype=%@",self.selectedMarkType];
			NSString *markDocString = [[SendHttpRequest sendHttpPostRequestWithURLString:urlString postString:postStr decodeEncoding:NSUTF8StringEncoding] retain];
			
			if ([[MyDataSource artMarkGetDataFromXMLDocument:markDocString IntoDB:NO] count] != 0) {
				[markArray release];
				markArray = [[MyDataSource artMarkGetDataFromXMLDocument:markDocString IntoDB:NO] retain];
				[self loadMarkScrollView:markArray Back:YES];
			}
			else {
				[self noDataAlertType:4];	
			}
			[markDocString release];
		}
	}
	
	else if([self showAlertWhileNoConnection]){
		if ([self.selectedMarkType isEqualToString:@"全部"]) {
			postStr = [NSString stringWithFormat:@"page=0&city=%@",self.selectedMarkCity];
		}
		else {
			postStr = [NSString stringWithFormat:@"page=0&city=%@&marktype=%@",self.selectedMarkCity,self.selectedMarkType];
		}
		
		NSString *markDocString = [[SendHttpRequest sendHttpPostRequestWithURLString:urlString postString:postStr decodeEncoding:NSUTF8StringEncoding] retain];
		
		if ([[MyDataSource artMarkGetDataFromXMLDocument:markDocString IntoDB:NO] count] != 0) {
			[markArray release];
			markArray = [[MyDataSource artMarkGetDataFromXMLDocument:markDocString IntoDB:NO] retain];

			[self loadMarkScrollView:markArray Back:YES];
		}
		else {
			[self noDataAlertType:4];	
		}
		[markDocString release];
	}
	
	
}
#pragma mark --------------------------------------------------
#pragma mark Left Cycle

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.	
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	eventView = nil;
	markView = nil;
	classTableView = nil;
	segmentControl = nil;
}


- (void)dealloc {
    [super dealloc];
	[eventScrollView release];
	[eventView release];
	
	[markScrollView release];
	[markView release];
	
	[classTableView release];
	
	[eventCityPickerBG release];
	[eventCityPickerView release];
	
	[eventTimePickerBG release];
	[eventTimePickerView release];
	
	[markCityPickerBG release];
	[markCityPickerView release];
	
	[markTypePickerBG release];
	[markTypePickerView release];
	
	[eventArray release];
	[markArray release];
	[yearArray release];
	[monthArray release];
	[dayArray release];
	
	[classInfoArray release];
	
	[segmentControl release];
}


- (id)init {
    if (self = [super init]) {
		currentClassPage = 0;
		currentMarkScrollPage = 0;
		currentEventScrollPage = 0;
		
		eventPostPage = 0;
		markPostPage = 0;
		
		isFirst = YES;
		reload = NO;
		
		self.selectedEventCity = @"全部";
		self.selectedMarkType = @"全部";
		self.selectedMarkCity = @"全部";
		
		allTimeFlag = YES;
		
		context = [Context shareContext];
    }
    return self;
}

//加载EventScrollView上面的图片
- (void)loadEventScrollView:(NSArray *)eventArr Back:(BOOL) back{
	
	for (UIView *subview in eventScrollView.subviews) {
		[subview removeFromSuperview];
	}
	
	for (int i = 0; i < [eventArr count]; i++){
		ArtEvent *event = [eventArr objectAtIndex:i];
		
		WebImageView *webImageView = [[WebImageView alloc] initWithFrame:CGRectMake(50 + 240*i, 0, 220, 236)];
		
		UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 220, 236)];
		[button addTarget:self action:@selector(gotoDetailView:) forControlEvents:UIControlEventTouchUpInside];
		[webImageView addSubview:button];
		button.tag = 2000+i;		
		[button release];

		
		[webImageView setImage:event._thumb];
		webImageView.titleLabel.text = [NSString stringWithFormat:@"%@\n%@ %@ 至 %@",event._title,event._city,event._starttime,event._endtime];
	    [webImageView setPushInfoCity:event];
		[eventScrollView addSubview:webImageView];
		[webImageView release];
		
	}
	[eventScrollView setContentSize:CGSizeMake(240*[eventArr count] + 100, eventScrollView.bounds.size.height)];
	if (back) {
		[eventScrollView setContentOffset:CGPointMake(0, 0)];
	}
}

//加载MarkScrollView上面的图片
- (void)loadMarkScrollView:(NSArray *)markArr Back:(BOOL) back{
	for (UIView *subview in markScrollView.subviews){
		[subview removeFromSuperview];
	}
		
	for (int i = 0; i < [markArr count]; i++) {
		ArtMark *mark = [markArr objectAtIndex:i];
		WebImageView *webImageView = [[WebImageView alloc] initWithFrame:CGRectMake(50 + 240*i, 0, 220, 236)];
		
		UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 220, 236)];
		[button addTarget:self action:@selector(gotoDetailView2:) forControlEvents:UIControlEventTouchUpInside];
		[webImageView addSubview:button];
		button.tag = 3000+i;	
		[button release];
		
		
		[webImageView setImage:mark._thumb];
		webImageView.titleLabel.text = [NSString stringWithFormat:@"%@\n%@",mark._title,mark._city];
        [markScrollView addSubview:webImageView];
		[webImageView release];
				
	}
	[markScrollView setContentSize:CGSizeMake(240*[markArr count] + 100, markScrollView.bounds.size.height)];
	if (back) {
		[markScrollView setContentOffset:CGPointMake(0, 0)];
	}

}

-(void)reloadAllData{
	
	//检测联网时才刷新
    Reachability *internetReach = [Reachability reachabilityForInternetConnection];
	[internetReach startNotifier];	
	NetworkStatus netStatus = [internetReach currentReachabilityStatus];
	if (netStatus) {
		
		reload = YES;
		NSString *urlstring = kArtEvent;
		NSString *postStr = @"page=0";
		NSString *documentStr = [[SendHttpRequest sendHttpPostRequestWithURLString:urlstring postString:postStr decodeEncoding:NSUTF8StringEncoding] retain];
        NSLog(@"xmstring : %@",documentStr);
		
		if ([[MyDataSource artEventGetDataFromXMLDocument:documentStr IntoDB:NO] count] != 0) {
			[eventArray release];
			eventArray = [[MyDataSource artEventGetDataFromXMLDocument:documentStr IntoDB:YES] retain];
			[self loadEventScrollView:eventArray Back:NO];
			[CacheManager setXML:documentStr withFileName:@"ArtEvent"];	
		}
		[documentStr release];
		
		NSString *markURLString = kArtMark;
		NSString *markPostStr = @"page=0";
		NSString *markDocString = [[SendHttpRequest sendHttpPostRequestWithURLString:markURLString postString:markPostStr decodeEncoding:NSUTF8StringEncoding] retain];
		
		if ([[MyDataSource artMarkGetDataFromXMLDocument:markDocString IntoDB:NO] count] != 0) {
			[markArray release];
			markArray = [[MyDataSource artMarkGetDataFromXMLDocument:markDocString IntoDB:YES] retain];
			[self loadEventScrollView:eventArray Back:NO];
			[CacheManager setXML:markDocString withFileName:@"ArtMark"];
		}
		[markDocString release];
		
		NSString *classURLString = kArtClass;
		NSString *postString = @"page=0";
		currentClassPage = 0;
		NSString *classDocString = [[SendHttpRequest sendHttpPostRequestWithURLString:classURLString postString:postString decodeEncoding:NSUTF8StringEncoding] retain];
		
		if ([[MyDataSource getArtClassInfo:classDocString] count] != 0) {
			[classInfoArray release];
			classInfoArray = [[MyDataSource getArtClassInfo:classDocString] retain];
			[classTableView reloadData];
			[CacheManager setXML:classDocString withFileName:@"ArtClass"];
		}
		
		[classDocString release];
	}
}

//每次启动，先加载缓存，当点击到该视图时，只刷新一边数据
- (void)viewDidShow{
	
	if (!reload) {
	reload = YES;
	OpenWaitView *wait = [[OpenWaitView alloc] initWithView:self.view];
	[wait setMode:MBProgressHUDModeIndeterminate];
	[wait setLabelText:@"正在刷新..."];
	[self.view addSubview:wait];
	[wait showWhileExecuting:@selector(reloadAllData) onTarget:self withObject:nil animated:YES];
	[wait release];
		
	}
	[eventCityPickerBG removeFromSuperview];
	[eventTimePickerBG removeFromSuperview];
	[markCityPickerBG removeFromSuperview];
	[markTypePickerBG removeFromSuperview];
	
}


- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.view.backgroundColor = [UIColor colorWithRed:0.79 green:0.78 blue:0.78 alpha:1.0];
	
	segmentControl = [[TESegmentedControl alloc] initWithFrame:CGRectMake(10, 5, 300, 30)];
	[segmentControl setDelegate:self];
	[segmentControl setItems:[NSArray arrayWithObjects:@"艺术活动",@"艺术地标",@"艺术课堂",nil]];
	[segmentControl setSelectedSegmentIndex:0];
	[self.view addSubview:segmentControl];
	[segmentControl release];
	
	//////////////////////////////////////////////////////////////////////////////
	//**********************************ArtEvent**********************************
	//////////////////////////////////////////////////////////////////////////////
	
	eventView = [[UIView alloc] initWithFrame:CGRectMake(0, 38, 320, 308)];
	[eventView setTag:101];
	[eventView setBackgroundColor:[UIColor clearColor]];
	eventView.layer.contents = (id)[UIImage imageNamed:@"background.png"].CGImage;
	[self.view addSubview:eventView];

	eventScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 10, 320, 240)];
	[eventScrollView setTag:1001];
	[eventScrollView setBackgroundColor:[UIColor clearColor]];	
	[eventScrollView setShowsHorizontalScrollIndicator:NO];
	[eventScrollView setDelegate:self];
	[eventView addSubview:eventScrollView];
	[eventScrollView release];
	
	Reachability *internetReach = [Reachability reachabilityForInternetConnection];
	[internetReach startNotifier];	
	NetworkStatus netStatus = [internetReach currentReachabilityStatus];
	if (netStatus) {
		NSLog(@"有网");
	}else {
		NSLog(@"没网");
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"本功能需要网络支持，请检查网络连接！"
													   delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
		[alert show];
		[alert release];
	}

	
	//下面两句话多余，数据在程序启动时已经加载
	NSString *xmlStr = [CacheManager getXMLStringWithFileName:@"ArtEvent"];
	eventArray = [[MyDataSource artEventGetDataFromXMLDocument:xmlStr IntoDB:YES] retain];
    
	[self loadEventScrollView:eventArray Back:YES];
	
	UIButton *cityButton = [UIButton buttonWithType:UIButtonTypeCustom];
	[cityButton setTag:5001];
	[cityButton setTitle:@"选择城市" forState:UIControlStateNormal];
	cityButton.frame = CGRectMake(45, 260, 90, 30);
	cityButton.titleLabel.font = [UIFont boldSystemFontOfSize:13];
	[cityButton setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
	[cityButton addTarget:self action:@selector(selectEventCity) forControlEvents:UIControlEventTouchUpInside];
	[cityButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
	[eventView addSubview:cityButton];
	
	UIButton *timeButton= [UIButton buttonWithType:UIButtonTypeCustom];
	[timeButton setTitle:@"选择时间" forState:UIControlStateNormal];
	timeButton.frame = CGRectMake(180, 260, 90, 30);
	timeButton.titleLabel.font = [UIFont boldSystemFontOfSize:13];
	[timeButton setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
	[timeButton addTarget:self action:@selector(selectEventTime) forControlEvents:UIControlEventTouchUpInside];
	[timeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
	[eventView addSubview:timeButton];
	
	//////////////////////////////////////////////////////////////////////////////
	//*********************************ArtMark**********************************
	//////////////////////////////////////////////////////////////////////////////

	markView = [[UIView alloc] initWithFrame:CGRectMake(0, 38, 320, 308)];
	[markView setTag:102];
	[markView setBackgroundColor:[UIColor clearColor]];
	markView.layer.contents = (id)[UIImage imageNamed:@"background.png"].CGImage;
	
	markScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 10, 320, 240)];
	[markScrollView setTag:1002];
	[markScrollView setBackgroundColor:[UIColor clearColor]];	
	[markScrollView setShowsHorizontalScrollIndicator:NO];
	[markScrollView setDelegate:self];
	[markView addSubview:markScrollView];
	[markScrollView release];
    
   
	NSString *xmlStrl = [CacheManager getXMLStringWithFileName:@"ArtMark"];
	markArray = [[MyDataSource artMarkGetDataFromXMLDocument:xmlStrl IntoDB:NO] retain]; 
	[self loadMarkScrollView:markArray Back:YES];
	
	UIButton *cityButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
	[cityButton1 setTag:5002];
	[cityButton1 setTitle:@"选择城市" forState:UIControlStateNormal];
	cityButton1.frame = CGRectMake(45, 260, 90, 30);
	cityButton1.titleLabel.font = [UIFont boldSystemFontOfSize:13];
	[cityButton1 setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
	[cityButton1 addTarget:self action:@selector(selectMarkCity) forControlEvents:UIControlEventTouchUpInside];
	[cityButton1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
	[markView addSubview:cityButton1];
	
	UIButton *typeButton= [UIButton buttonWithType:UIButtonTypeCustom];
	[typeButton setTitle:@"选择类型" forState:UIControlStateNormal];
	typeButton.frame = CGRectMake(180, 260, 90, 30);
	typeButton.titleLabel.font = [UIFont boldSystemFontOfSize:13];
	[typeButton setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
	[typeButton addTarget:self action:@selector(selectMarkType) forControlEvents:UIControlEventTouchUpInside];
	[typeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
	[markView addSubview:typeButton];
	
	//////////////////////////////////////////////////////////////////////////////
	//*********************************ArtClass**********************************
	//////////////////////////////////////////////////////////////////////////////
	classTableView = [[UITableView alloc] initWithFrame:CGRectMake(5, 40, 310, 305) style:UITableViewStylePlain];
	[classTableView setTag:103];
	classTableView.backgroundColor = [UIColor clearColor];
	classTableView.layer.contents = (id)[UIImage imageNamed:@"tableViewBackground.png"].CGImage;
	classTableView.layer.cornerRadius = 8;
	classTableView.layer.masksToBounds = YES;
	[classTableView setSeparatorColor:[UIColor clearColor]];
	[classTableView setDataSource:self];
	[classTableView setDelegate:self];
	classTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(5, 10, 310, 40)];
	
	
	[classInfoArray release];
	NSString *xmlString = [CacheManager getXMLStringWithFileName:@"ArtClass"];
	classInfoArray = [[MyDataSource getArtClassInfo:xmlString] retain]; 
	
	waitView = [[OpenWaitView alloc] initWithView:self.view];
	[waitView setLabelText:@"正在查询..."];
	[waitView setMode:MBProgressHUDModeIndeterminate];
	[self.view addSubview:waitView];
	[waitView release];
	
	loading = [[OpenWaitView alloc] initWithView:self.view];
	[loading setMode:MBProgressHUDModeIndeterminate];
	[loading setLabelText:@"加载更多内容..."];
	[self.view addSubview:loading];
	[loading release];
	
}


#pragma mark --------------------------------------------------
#pragma mark UIScrollView Delegate

- (void)eventScrollViewScrollToCityIndex:(NSInteger) index{
	[eventScrollView setContentOffset:CGPointMake(index * 240, 0) animated:NO];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    switch (scrollView.tag) {
		case 1001:{
			CGFloat pageWidth = 240;
			int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
			currentEventScrollPage = page;
			break;
		}
		case 1002:
		{
			CGFloat pageWidth = 240;
			int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
			currentMarkScrollPage = page;		
			break;
		}
		default:
			break;
	}
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
	
	Reachability *internetReach = [Reachability reachabilityForInternetConnection];
	[internetReach startNotifier];	
	NetworkStatus netStatus = [internetReach currentReachabilityStatus];
	
	if (netStatus) {
		switch (scrollView.tag) {
			case 1001:
				if (currentEventScrollPage == 0) {
					[self.view bringSubviewToFront:loading];
					[loading showWhileExecuting:@selector(loadEventsLeft) onTarget:self withObject:nil animated:NO];
				}
				else if (currentEventScrollPage == [eventArray count] - 1) {
					[self.view bringSubviewToFront:loading];
					[loading showWhileExecuting:@selector(loadEventsRight) onTarget:self withObject:nil animated:NO];
				}
				break;
			case 1002:
				if (currentMarkScrollPage == 0) {
					[self.view bringSubviewToFront:loading];
					[loading showWhileExecuting:@selector(loadMarksLeft) onTarget:self withObject:nil animated:YES];
				}
				else if (currentMarkScrollPage == [markArray count] - 1) {
					[self.view bringSubviewToFront:loading];
					[loading showWhileExecuting:@selector(loadMarksRight) onTarget:self withObject:nil animated:YES];
				}
				break;
			default:
				break;
		}
	}
}




-(void)loadEventsRight{
	eventPostPage++;
	
	if (eventPostPage < context.artEventTotalPages) {
		NSString *beginStr = [NSString stringWithFormat:@"%04d-%02d-%02d",selectedBeginYear,selectedBeginMonth,selectedBeginDay];
		NSString *endStr = [NSString stringWithFormat:@"%04d-%02d-%02d",selectedEndYear,selectedEndMonth,selectedEndDay];
		
		NSString *urlstring = nil;
		NSString *postStr = nil;
		
		if ([self.selectedEventCity isEqualToString:@"全部"]) {
			if ([beginStr isEqualToString:@"0000-00-00"]) {
				urlstring = kArtEvent;
				postStr = [NSString stringWithFormat:@"page=%d",eventPostPage]; 
			}
			else {
				urlstring = kArtEventSearch;
				postStr = [NSString stringWithFormat:@"page=%d&starttime=%@&endtime=%@",eventPostPage,beginStr,endStr];
			}
		}
		else {
			urlstring = kArtEventSearch;
			if ([beginStr isEqualToString:@"0000-00-00"]) {
				postStr = [NSString stringWithFormat:@"page=%d&city=%@",eventPostPage,self.selectedEventCity];
			}
			else {
				postStr = [NSString stringWithFormat:@"page=%d&city=%@&starttime=%@&endtime=%@",eventPostPage,self.selectedEventCity,beginStr,endStr];
			}
			
		}
		
		NSString *documentStr = [[SendHttpRequest sendHttpPostRequestWithURLString:urlstring postString:postStr decodeEncoding:NSUTF8StringEncoding] retain];
		
		if ([[MyDataSource artEventGetDataFromXMLDocument:documentStr IntoDB:NO] count] != 0) {
			[eventArray release];
			eventArray = [[MyDataSource artEventGetDataFromXMLDocument:documentStr IntoDB:NO] retain];
			[self loadEventScrollView:eventArray Back:YES];
		}
		
		[documentStr release];
		
	}else {
		eventPostPage--;
	}


	NSLog(@"eventPostPage = %d",eventPostPage);

}


- (void)loadEventsLeft{
	eventPostPage--;
	
	if (eventPostPage >= 0) {
				
		NSString *beginStr = [NSString stringWithFormat:@"%04d-%02d-%02d",selectedBeginYear,selectedBeginMonth,selectedBeginDay];
		NSString *endStr = [NSString stringWithFormat:@"%04d-%02d-%02d",selectedEndYear,selectedEndMonth,selectedEndDay];
		
		
		NSString *urlstring = nil;
		NSString *postStr = nil;
		
		if ([self.selectedEventCity isEqualToString:@"全部"]) {
			if ([beginStr isEqualToString:@"0000-00-00"]) {
				urlstring = kArtEvent;
				postStr = [NSString stringWithFormat:@"page=%d",eventPostPage]; 
			}
			else {
				urlstring = kArtEventSearch;
				postStr = [NSString stringWithFormat:@"page=%d&starttime=%@&endtime=%@",eventPostPage,beginStr,endStr];
			}
		}
		else {
			urlstring = kArtEventSearch;
			if ([beginStr isEqualToString:@"0000-00-00"]) {
				postStr = [NSString stringWithFormat:@"page=%d&city=%@",eventPostPage,self.selectedEventCity];
			}
			else {
				postStr = [NSString stringWithFormat:@"page=%d&city=%@&starttime=%@&endtime=%@",eventPostPage,self.selectedEventCity,beginStr,endStr];
			}
			
		}
		
		NSString *documentStr = [[SendHttpRequest sendHttpPostRequestWithURLString:urlstring postString:postStr decodeEncoding:NSUTF8StringEncoding] retain];
		
		if ([[MyDataSource artEventGetDataFromXMLDocument:documentStr IntoDB:NO] count] != 0) {
			[eventArray release];
			eventArray = [[MyDataSource artEventGetDataFromXMLDocument:documentStr IntoDB:NO] retain];
			[self loadEventScrollView:eventArray Back:YES];
		}
		[documentStr release];
	}
	else {
		eventPostPage = 0;
	}
	NSLog(@"eventPostPage = %d",eventPostPage);

}


-(void)loadMarksRight{
	markPostPage++;
	
	
	if (markPostPage < context.artMarkTotalPages) {
		NSString *urlString = nil;
		NSString *postStr = nil;
		
		if ([self.selectedMarkCity isEqualToString:@"全部"]) {
			if ([self.selectedMarkType isEqualToString:@"全部"]) {
				urlString = kArtMark;
				postStr = [NSString stringWithFormat:@"page=%d",markPostPage];
			}
			else {
				urlString = [NSString stringWithFormat:kArtMarkSearch];
				postStr = [NSString stringWithFormat:@"page=%d&marktype=%@",markPostPage];
			}
		}
		else {
			
			urlString = [NSString stringWithFormat:kArtMarkSearch];
			if ([self.selectedMarkType isEqualToString:@"全部"]) {
				postStr = [NSString stringWithFormat:@"page=%d&city=%@",markPostPage,self.selectedMarkCity];
			}
			else {
				postStr = [NSString stringWithFormat:@"page=%d&city=%@&marktype=%@",markPostPage,self.selectedMarkCity,self.selectedMarkType];
			}
			
		}
		
		NSString *documentStr = [[SendHttpRequest sendHttpPostRequestWithURLString:urlString postString:postStr decodeEncoding:NSUTF8StringEncoding] retain];
		
		
		NSLog(@"%@",documentStr);
		
		if ([[MyDataSource artMarkGetDataFromXMLDocument:documentStr IntoDB:NO] count] != 0) {
			[markArray release];
			markArray = [[MyDataSource artMarkGetDataFromXMLDocument:documentStr IntoDB:NO] retain];
			[self loadMarkScrollView:markArray Back:YES];
		}
		[documentStr release];
	}
	else {
		markPostPage--;
	}
    NSLog(@"markPostPage = %d",markPostPage);
}

- (void)loadMarksLeft{
	markPostPage--;
	
	if (markPostPage >= 0) {
		NSString *urlString = nil;
		NSString *postStr = nil;
		
		if ([self.selectedMarkCity isEqualToString:@"全部"]) {
			if ([self.selectedMarkType isEqualToString:@"全部"]) {
				urlString = kArtMark;
				postStr = [NSString stringWithFormat:@"page=%d",markPostPage];
			}
			else {
				urlString = [NSString stringWithFormat:kArtMarkSearch];
				postStr = [NSString stringWithFormat:@"page=%d&marktype=%@",markPostPage];
			}
		}
		else {
			
			urlString = [NSString stringWithFormat:kArtMarkSearch];
			if ([self.selectedMarkType isEqualToString:@"全部"]) {
				postStr = [NSString stringWithFormat:@"page=%d&city=%@",markPostPage,self.selectedMarkCity];
			}
			else {
				postStr = [NSString stringWithFormat:@"page=%d&city=%@&marktype=%@",markPostPage,self.selectedMarkCity,self.selectedMarkType];
			}
			
		}
		
		NSString *documentStr = [[SendHttpRequest sendHttpPostRequestWithURLString:urlString postString:postStr decodeEncoding:NSUTF8StringEncoding] retain];
		
		if ([[MyDataSource artMarkGetDataFromXMLDocument:documentStr IntoDB:NO] count] != 0) {
			[markArray release];
			markArray = [[MyDataSource artMarkGetDataFromXMLDocument:documentStr IntoDB:NO] retain];
			[self loadMarkScrollView:markArray Back:YES];
		}
		[documentStr release];
	}
	else {
		markPostPage = 0;
	}
    NSLog(@"markPostPage = %d",markPostPage);
}


#pragma mark -------------------------------------------------
#pragma mark UITableView Delegate & DataSource


- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{
	return [classInfoArray count];
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	
	static NSString *cutoutIdentifier = @"CutoutCell";
	
	MyTableViewCell *cell = (MyTableViewCell *)[aTableView dequeueReusableCellWithIdentifier:cutoutIdentifier];
	if (cell == nil) {
		cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cutoutIdentifier] autorelease];
		cell.accessoryType = UITableViewCellAccessoryNone;
		[cell setSelectionStyle:UITableViewCellSelectionStyleNone];	
	}
	

	cell.delegate = self;
	[cell setTag:indexPath.row];
	ArtClass *class = [classInfoArray objectAtIndex:indexPath.row];
	[cell setImageWithURL:class._thumb];
	[cell setTitleLabelText:class._title];
	[cell setDetailLabelText:[NSString stringWithFormat:@"%@\n  \n  ",class._pubdate]];
	
	
	return cell;
}	

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	
	if ([self showAlertWhileNoConnection]) {
        
		DetailWebViewController *classViewController = [[DetailWebViewController alloc] init];
		NSString *htmlStr = [[classInfoArray objectAtIndex:indexPath.row ] _content];
		NSLog(@"%s,%d, %@", __FUNCTION__, __LINE__, htmlStr);
		[classViewController.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:htmlStr]]];
		[self.navigationController pushViewController:classViewController animated:YES];
		[classViewController release];
	}

	
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{	
	return 130;
}

#pragma mark -----------------------------------------------
#pragma mark MyTableViewCell Delegate
-(void)seeDetailInfo:(NSInteger) index{
    
	if ([self showAlertWhileNoConnection]) {
		DetailWebViewController *classViewController = [[DetailWebViewController alloc] init];
		NSString *htmlStr = [[classInfoArray objectAtIndex:index] _content];
		
		[classViewController.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:htmlStr]]];	
		[self.navigationController pushViewController:classViewController animated:YES];
		[classViewController release];
		
	}
}

#pragma mark ------------------------------------------------
#pragma mark UIPickerViewDataSource

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
	switch (pickerView.tag) {
		case kEventCityPicker:
        case kMarkCityPicker:
		case kMarkTypePicker:
			return 200;
			break;
        case kEventTimePicker:
			switch (component) {
				case 0:
					return 80;
					break;
				case 1:
				case 2:
					return 50;
					break;
				default:
					break;
			}
			break;
		default:
			break;
	}
	return 0;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
	switch (pickerView.tag) {
		case kEventCityPicker:
		case kMarkCityPicker:
			return [cityArray count];
			break;
		case kEventTimePicker:
		{    
			switch (component) {
				case 0:
					return [yearArray count];
					break;
				case 1:
					return [monthArray count];
					break;
                case 2:
					return [dayArray count];
					break;
				default:
					break;
			}
			break;
		}
		case kMarkTypePicker:
			return [typeArray count];
			break;

		default:
			break;
	}
	return 0;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
	if (pickerView.tag == kEventTimePicker) {
		return 3;
	}
	return 1;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
	switch (pickerView.tag) {
		case kEventCityPicker:
			self.selectedEventCity = [cityArray objectAtIndex:row];
			break;
		case kEventTimePicker:
		{
			if (isFirst) {
				switch (component) {
					case 0:
						selectedBeginYear = [[yearArray objectAtIndex:row] intValue];
						break;
					case 1:
					{
						selectedBeginMonth = [[monthArray objectAtIndex:row] intValue];
						[self reloadTimePickerDayComponent:selectedBeginMonth];
						break;
					}
                    case 2:
						selectedBeginDay = [[dayArray objectAtIndex:row] intValue];
						break;
					default:
						break;
				}
			}
			else 
			{
				switch (component) {
					case 0:
						selectedEndYear = [[yearArray objectAtIndex:row] intValue];
						break;
					case 1:
					{
						selectedEndMonth = [[monthArray objectAtIndex:row] intValue];
						[self reloadTimePickerDayComponent:selectedEndMonth];
						break;
					}
					case 2:
						selectedEndDay = [[dayArray objectAtIndex:row] intValue];
						break;

					default:
						break;
				}
			}
			break;
		}
		case kMarkCityPicker:
			self.selectedMarkCity = [cityArray objectAtIndex:row];
        	break;
        case kMarkTypePicker:
			self.selectedMarkType = [typeArray objectAtIndex:row];
			break;
		default:
			break;
	}
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row 
		  forComponent:(NSInteger)component reusingView:(UIView *)view{
	
		UILabel *titleLabel = [[UILabel alloc] init];
		[titleLabel setFont:[UIFont systemFontOfSize:22]];
		[titleLabel setBackgroundColor:[UIColor clearColor]];
	
		if(pickerView.tag == kEventCityPicker || pickerView.tag == kMarkCityPicker){
			titleLabel.frame = CGRectMake(70, 0, 60, 40);
			[titleLabel setText:[cityArray objectAtIndex:row]];	
		}else if (pickerView.tag == kMarkTypePicker) {
			titleLabel.frame = CGRectMake(50, 0, 100, 40);
			[titleLabel setText:[typeArray objectAtIndex:row]];
		}else {
			switch (component) {
				case 0:
					titleLabel.frame = CGRectMake(20, 0, 60, 40);
					[titleLabel setText:[yearArray objectAtIndex:row]];
					break;
				case 1:
					titleLabel.frame = CGRectMake(10, 0, 30, 40);
					[titleLabel setText:[monthArray objectAtIndex:row]];
					break;
                case 2:
					titleLabel.frame = CGRectMake(10, 0, 30, 40);
					[titleLabel setText:[dayArray objectAtIndex:row]];
					break;
				default:
					break;
			}
		}
		return [titleLabel autorelease];
}


@end
